﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FLink_Index.aspx.cs" Inherits="ncp.Web.cms.work.FLinkManager.FLink_Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../themes/icon.css" />
    <link href="../../css/base.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="../../js/jquery-1.7.1.min.js"></script>
	<script type="text/javascript" src="../../js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../js/common.js"></script>

</head>
<body style="margin:0; padding:0px;">
<form id="form1" runat="server">
    <div>
        <div class="toolbar" style="background-color:#efefef">
	        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-add" onclick="onAddNews();">增加</a>
	        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-remove" onclick="onDeleteNews();">删除</a>
	        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-edit" onclick="onEditNews();">编辑</a>
	        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-reload" onclick="onSetNewsStatus()">禁用/启用</a>
        </div>
        <div id="divright">    
        </div>
	    <div id="divNewsInfoBox">
	        <div class="form mod-content">
            <table>
                <tr>
                    <td class="mod-label"> <label for="webAddr">网址:</label></td>
                    <td>
                    <input type="hidden" id="newsId"/>
                    <input type="text" class="bh-input-long" style="width: 400px" maxlength="100" id="webAddr" value="http://"/>
                    <span class="msg"></span>
                    </td>
                </tr>
                <tr>
                    <td class="mod-label"> <label for="linkName">网址名称:</label></td>
                    <td>
                    <input type="text" class="bh-input-long" style="width: 400px" maxlength="100" id="linkName"/>
                    <span class="msg"></span>
                    </td>
                </tr>
                <tr>
                    <td class="mod-label"> <label for="sort">排列位置:</label></td>
                    <td>
                    <input type="text" class="bh-input-small" maxlength="10" id="sort" value="1"/>(数值越小越靠前)
                    <span class="msg"></span>
                    </td>
                </tr>
                <tr>
                    <td class="mod-label" style="vertical-align:top;"><label for="description">网站简介:</label></td>
                    <td>
                        <textarea class="description" name="description" onkeyup="this.value = this.value.substring(0, 127)" rows="5" cols="3" id="description"></textarea>
                    </td>
                </tr>
                <tr>
            <td colspan="2" style="text-align: center;border-bottom: none;">
                <a href="javascript:void(0)" class="easyui-linkbutton" onclick="onSaveNews()">确定</a>&nbsp;&nbsp;
	            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="onCloseNewsWindow()">关闭</a>
            </td>
        </tr>
            </table>
        </div>
	    </div>
	</div>
	</form>
</body>
</html>
<script type="text/javascript">
    $(function() { //初始化
        initDialog();
    });
    
    //关闭新闻发布窗口
    function onCloseNewsWindow() {
        emptyData();
        $('#divNewsInfoBox').window('close');
    }
    //清空数据
    function emptyData() {
        $("#linkName,#description").val("");
        $("#sort").val(1);
        $("#webAddr").val("http://");
    }
    
    //初始化对话框
    function initDialog() {
        $('#divNewsInfoBox').dialog({
            title: '友情链接',
            modal: true,
            width: 560,
            height: 280,
            left: 100,
            top: 100,
            padding: 10,
            closed: true,
            draggable: true,
                closable:true
        });
    }

</script>

<%--商品信息--%>
<script type="text/javascript">
    var pageobj = new Object();

    $(function() {
        initPageObj();
        initDatagrid();
        onSearch();
    });

   
    //初始化页码对象
    function initPageObj() {
        pageobj.currentpageindex = 1;
        pageobj.pagesize = 10;
        pageobj.sorting = "Link_Order";
        pageobj.sortdir = "asc";
        pageobj.recordcount = GetRecordCount();
    }

    //初始化表格
    function initDatagrid() {
        $('#divright').datagrid({
        height: $(window).height() - 61,
            nowrap: true,
            striped: true,
            singleSelect: true,
            sortName: 'Link_Order',
            sortOrder: 'asc',
            remoteSort: false,
            idField: 'Link_Id',
            pagination: true,
            rownumbers: true,
            columns: [[
					{ field: 'Link_Id', title: '编号', hidden: 'true', width: 80 },
					{ field: 'Link_Name', title: '网站名称', sortable: 'true', width: 300 },
					{ field: 'Link_Addr', title: '链接地址', sortable: 'false', width: 300,
					    formatter: function(val, rec) {
					        return "<a href=\""+val+"\" target=\"_blank\">"+val+"</a>";
					    }
					},
					{ field: 'Link_Order', title: '排序', sortable: 'true', width: 130 },
					{ field: 'Link_Status', title: '状态', sortable: 'true', width: 60, align: 'center',
					    styler: function(value, row, index) {
					        if (value == '0') {
					            return 'color:red;font-weight:bold;';
					        }
					    },
					    formatter: function(val, rec) {
					        if (val == '0') {
					            return "禁用";
					        } else {
					        return "启用";
					        }
					    }
					}
				]]
        });
    }


    function initPager() {
        var p = $('#divright').datagrid('getPager');
        $(p).pagination({
            total: pageobj.recordcount,
            pageSize: pageobj.pagesize,
            pageList: [10, 15, 20, 25, 30],
            showRefresh: true,
            showPageList:true,
            beforePageText: '第',
            afterPageText: '页 共 {pages} 页',
            displayMsg: '当前显示 {from} - {to} 条记录 共 {total} 条记录',
            onSelectPage: function(pageNumber, pageSize) {
                $(this).pagination('loading');
                onShowPageNo(pageNumber, pageSize);
                $(this).pagination('loaded');
            }
        });
    }
    
    //翻页
    function onShowPageNo(pageNumber, pageSize) {
        pageobj.pagesize = pageSize;
        pageobj.currentpageindex = pageNumber;
        LoadNewsInfo();
        initPager();
        return false;
    }

    //得到总记录数
    function GetRecordCount() {
        var response = ncp.Web.cms.work.FLinkManager.FLink_Index.GetRecordCount();
        if (response.value != null) {    
            return response.value;
        }
        else
            return 0;
    }

    //查询
    function onSearch() {
        pageobj.currentpageindex = 1;
        pageobj.recordcount = GetRecordCount();
        LoadNewsInfo();
        initPager();
        return false;
    }

    //装载商品信息视图
    function LoadNewsInfo() {
        var jsonStr = $.objectToJson(pageobj);

        var response = ncp.Web.cms.work.FLinkManager.FLink_Index.BindTable(jsonStr);
        if (response.value != null) {
            var data = eval(response.value);
            $('#divright').datagrid('loadData', data);
        }
        $('#divright').datagrid('clearSelections');
    }

    //增加新闻
    function onAddNews() {
        $("#newsId").val("");
        $('#divNewsInfoBox').dialog('setTitle', '添加链接');
        $('#divNewsInfoBox').dialog("open");
        emptyData();
        return false;
    }
    
    //保存新闻
    function onSaveNews() {
        //执行上传
        var obj = new Object();
        obj.Link_Id = $("#newsId").val();
        obj.Link_Name = $("#linkName").val();
        obj.Link_Addr = $('#webAddr').val();
        obj.Link_Info = $("#description").val();
        obj.Link_Order = $("#sort").val();
        if ($.trim(obj.Link_Name)=="") {
            alert("网站名称不能为空！");
            return;
        }
        if ($.trim(obj.Link_Addr)=="") {
            alert("链接地址不能为空！");
            return;
        }
        if (isNaN(obj.Link_Order)) {
            alert("排列位置必须为数字！");
            return;
        }
        var response = ncp.Web.cms.work.FLinkManager.FLink_Index.OnSave($.objectToJson(obj));
        
        if (response.value != "") {
            var jsonOBJ = eval(response.value)[0];
            alert(jsonOBJ.msg);
            if (jsonOBJ.isreload) {
                onSearch();
                onCloseNewsWindow();
                emptyData();
            }
        }
    }

    //禁用启用文章
    function onSetNewsStatus() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            var newsId = selected.Link_Id;
            var status = selected.Link_Status == 0 ? 1 : 0;
            var response = ncp.Web.cms.work.FLinkManager.FLink_Index.SetNewsStatus(newsId,status);
            if (response.value != "") {
                var jsonOBJ = eval(response.value)[0];
                alert(jsonOBJ.msg);
                if (jsonOBJ.isreload) {
                    LoadNewsInfo();
                }
            }
        }
        else
            alert("请先选择一条记录！");
    }

    //编辑商品
    function onEditNews() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            $('#divNewsInfoBox').dialog('setTitle', '编辑链接');
            $('#divNewsInfoBox').dialog("open");
            $("#newsId").val(selected.Link_Id);
            $("#linkName").val(selected.Link_Name);
            $('#webAddr').val(selected.Link_Addr);
            $("#description").val(selected.Link_Info);
            $("#sort").val(selected.Link_Order);
        }
        else
            alert("请先选择一条记录！");
        return false;
    }

    //删除商品
    function onDeleteNews() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            if (confirm("您确定要删除吗？")) {
                var response = ncp.Web.cms.work.FLinkManager.FLink_Index.DeleteNewsInfo(selected.Link_Id);
                if (response.value != null) {
                    var jsonOBJ = eval(response.value)[0];
                    if (jsonOBJ.isreload) {
                        var index = $('#divright').datagrid('getRowIndex', selected);
                        $('#divright').datagrid('deleteRow', index);
                        $('#divright').datagrid('clearSelections');
                    }
                    alert(jsonOBJ.msg);
                }
            } 
        }
        else
            alert("请先选择一条记录！");
        return false;
    }
</script>
